package com.utils.conmponentbase.tools;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

/**
 * MvvmDemo com.utils.conmponentbase.tools
 * 2020-01-03 14:34
 */
public class FragmentTools {
    private Fragment mCurrentFragment;

    /**
     * 显示fragment
     *
     * @param frameLayoutId frameLayoutId
     * @param fragment      fragment
     */
    private void addFragment(AppCompatActivity pActivity, int frameLayoutId, Fragment fragment, String tag) {
        if (fragment != null) {
            FragmentTransaction transaction = pActivity.getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment).show(fragment);
                } else {
                    transaction.show(fragment);
                }
            } else {
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment).add(frameLayoutId, fragment, tag);
                } else {
                    transaction.add(frameLayoutId, fragment);
                }
            }
            mCurrentFragment = fragment;
            transaction.commitAllowingStateLoss();
        }
    }
}
